package main

//go package官网中gorm.io包的示例代码 搬来使用
import (
	"fmt"
	"ginchat/models"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {
	//
	// //配置mysql连接参数
	username := "root"        //账号
	password := "17727096201" //mysql的密码
	host := "127.0.0.1"       //本机地址
	port := 3306
	Dbname := "ginchat"
	timeout := "10s"

	ds := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local&timeout=%s", username, password, host, port, Dbname, timeout)
	db, err := gorm.Open(mysql.Open(ds), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// Migrate the schema
	db.AutoMigrate(&models.Contact{})
	db.AutoMigrate(&models.Group_Message{})
	db.AutoMigrate(&models.Message{})
	db.AutoMigrate(&models.UserBasic{})

	// Create
	// user := &models.UserBasic{}
	// user.Name = "中专"
	// db.Create(user)
	// fmt.Println(user.ID)

	// // Read
	// fmt.Println(db.First(user)) // find product with integer primary key

	// // Update - update product's price to 200
	// db.Model(user).Update("PassWord", "1234")
	// Update - update multiple fields
	// db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
	// db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	// Delete - delete product
	// db.Delete(&product, 1)
}
